﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model.Group;
using YouHP.Model;

namespace YouHP.Pages.User
{
    public class CommentEdit : YouHP.Pages.User.UserBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected CommentInfo CommentData;

        protected void HandleCommon()
        {
            this.SetTitle("修改留言");
            this.AddLocation("修改留言", "");


            int id = RequestTools.GetInt("id", 0);
            if (id <= 0)
            {
                this.AddErrLine("无效的参数 id");
                return;
            }

            CommentData = CommentBLL.Instance.GetCommentInfo(id);

            if (CommentData.PostUserId != this.UserData.Id)
            {
                this.AddErrLine("您没有权限该修改回复");
                return;
            }
        }

        protected void HandlePost()
        {
            CommentInfo info = ValidateForm();
            if (ErrorCount > 0) return;
            bool succ = CommentBLL.Instance.UpdateComment(info);
            if (succ == false)
            {
                this.AddErrLine("修改留言失败");
            }
            else
            {
                this.AddMsgLine("修改留言成功");
            }
            this.SetUrl("/User/CommentList.aspx");
            this.SetMetaRefresh();
        }

        protected void HandleGet()
        {
        }

        CommentInfo ValidateForm()
        {
            CommentInfo info = CommentData;
            
            info.Body = RequestTools.GetFormString("msg").Trim();
            if (info.Body.Length < 2)
            {
                this.AddErrLine("留言长度不能小于 2 ");
                return null;
            }
            info.UpdateTime = DateTime.Now;
            
            return info;
        }
    }
}